package com.yiqi.zhiyuan.feature.mine.view;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.enums.CertificateType;
import com.yiqi.zhiyuan.entity.vo.DiverCenterVO;
import com.yiqi.zhiyuan.feature.certification.view.AuthenticationStateActivity;
import com.yiqi.zhiyuan.feature.certification.view.UserInformationActivity;
import com.yiqi.zhiyuan.feature.login.view.LoginActivity;
import com.yiqi.zhiyuan.feature.main.view.UserInfoActivity;
import com.yiqi.zhiyuan.feature.mine.contract.MineContract;
import com.yiqi.zhiyuan.feature.mine.dependencies.mine.DaggerMineComponent;
import com.yiqi.zhiyuan.feature.mine.dependencies.mine.MinePresenterModule;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements MineContract.View {

    @BindView(R.id.img_mine_user_head)
    ImageView mImgMineUserHead;
    @BindView(R.id.tv_mine_user_name)
    TextView mTvMineUserName;
    @BindView(R.id.tv_car_name_color)
    TextView mTvCarNameColor;
    @BindView(R.id.tv_car_number)
    TextView mTvCarNumber;
    @BindView(R.id.tv_mine_money)
    TextView mTvMineMoney;
    @BindView(R.id.tv_mine_my_integral)
    TextView mTvMineMyIntegral;
    @BindView(R.id.rl_mine_car)
    RelativeLayout mRlMineCar;
    @BindView(R.id.rl_mine_safety_certificate)
    RelativeLayout mRlMineSafetyCertificate;
    @BindView(R.id.rl_mine_setting)
    RelativeLayout mRlMineSetting;
    @BindView(R.id.rl_mine_feedback)
    RelativeLayout mRlMineFeedback;
    @BindView(R.id.rl_mine_about)
    RelativeLayout mRlMineAbout;
    @BindView(R.id.rl_mine_customer_service)
    RelativeLayout mRlMineCustomerService;
    @BindView(R.id.tv_car_name_type)
    TextView mTvCarNameType;
    @BindView(R.id.tv_mine_cer_stauts)
    TextView mTvMineCerStauts;
    @BindView(R.id.tv_mine_no_see)
    TextView mTvMineNoSee;

    @BindView(R.id.iv_score_look)
    ImageView mIvScoreLook;

    private Integer isPass;
    private String mTitle;
    private boolean isLooked = true;
    @Inject
    MineContract.Presenter mPresenter;

    public static MineFragment getInstance(String title) {
        MineFragment sf = new MineFragment();
        sf.mTitle = title;
        return sf;
    }


    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initInjector() {
        DaggerMineComponent.builder().minePresenterModule(new MinePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onStart() {
        super.onStart();
        mPresenter.doGetDiverCenter();
    }


    @Override
    protected void initEvent() {

    }


    @OnClick({R.id.tv_mine_money, R.id.rl_mine_car, R.id.rl_mine_setting, R.id.rl_mine_feedback,
            R.id.rl_mine_about, R.id.rl_mine_customer_service, R.id.rl_user_info, R.id.tv_mine_my_integral})
    public void onViewClicked(View view) {
        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }

        Intent intent = null;
        switch (view.getId()) {
            case R.id.tv_mine_money:
                break;
            case R.id.tv_mine_my_integral:
                intent=new Intent(getActivity(), MyIntegralActivity.class);
                intent.putExtra("user_info", mCenterVO);
                break;
            case R.id.rl_mine_car:
                startActivity(new Intent(getActivity(), MyVehicleActivity.class));
                break;
            case R.id.rl_mine_setting:
                intent=new Intent(getActivity(), SettingActivity.class);
                intent.putExtra("user_info", mCenterVO);
                break;
            case R.id.rl_mine_feedback:
                startActivity(new Intent(getActivity(), FeedBackActivity.class));
                break;
            case R.id.rl_mine_about:
                startActivity(new Intent(getActivity(), ActivityAbout.class));
                break;
            case R.id.rl_mine_customer_service:
                showMsg("此功能正在开发中...");
                break;
            case R.id.rl_user_info:
                intent = new Intent(getActivity(), UserInfoActivity.class);
                intent.putExtra("user_info", mCenterVO);
                break;
        }

        if (intent != null) {
            startActivity(intent);
        }
    }

    DiverCenterVO mCenterVO;

    @Override
    public void showDiverCenterSuccess(DiverCenterVO centerVO) {
        mCenterVO = centerVO;
        mTvMineUserName.setText(centerVO.getUsername());
        mTvCarNameType.setText(TextUtils.isEmpty(centerVO.getCarName()) ? "" : centerVO.getCarModelNo() + (TextUtils.isEmpty(centerVO.getCarModelNo()) ? "" : centerVO.getCarModelNo()));
        mTvCarNameColor.setText(TextUtils.isEmpty(centerVO.getCarColor()) ? "" : String.format("(%s)", centerVO.getCarColor()));
        mTvCarNumber.setText(TextUtils.isEmpty(centerVO.getCarNo()) ? "--" : centerVO.getCarNo());
        AM.image().bindToCircleObject(centerVO.getAvatarUrl(), mImgMineUserHead);
        mTvMineMoney.setText(TextUtils.isEmpty(centerVO.getAccumulatedPointsMonth()) ? "--" : centerVO.getAccumulatedPointsMonth());
        mTvMineCerStauts.setText(centerVO.getIsPass() == null ? "去认证" : CertificateType.getName(centerVO.getIsPass()));
        isPass = centerVO.getIsPass();
        isLooked = AM.user().isAllowLook();
        setEyes(isLooked);
    }

    @Override
    public void showDiverCenterError(String errorMsg) {
        showMsg(errorMsg);
    }


    /**
     * 司机认证
     */

    @OnClick(R.id.rl_mine_safety_certificate)
    public void onViewCertificateClicked() {
        Intent intent = null;
        if (isPass == null) {
            intent = new Intent(getActivity(), UserInformationActivity.class);
        } else if (isPass == CertificateType.CERTIFICATION_NONE.getIndex()) {
            intent = new Intent(getActivity(), UserInformationActivity.class);
        } else {
            intent = new Intent(getActivity(), AuthenticationStateActivity.class);
        }

        if (intent != null&&mCenterVO!=null) {
            intent.putExtra("auth_state", isPass);
            intent.putExtra("is_first", false);
            intent.putExtra("no_reason", mCenterVO.getNoPassReason());
            startActivity(intent);
        }
    }


    @OnClick(R.id.btn_score_look)
    public void onViewLookClicked() {
        isLooked = !isLooked;
        AM.user().setAllowLook(isLooked);
        mIvScoreLook.setImageResource(isLooked ? R.mipmap.ic_login_pwd_select : R.mipmap.ic_mine_no_see);
        setEyes(isLooked);
    }


    private void setEyes(boolean islook) {
        mTvMineNoSee.setVisibility(islook ? View.GONE : View.VISIBLE);
        mTvMineMoney.setVisibility(islook ? View.VISIBLE : View.GONE);
    }

}
